<template>
  <div class="login-container">
    <div class="login-detail">
      <a-form-model
        ref="form"
        :rules="rules"
        :model="loginForm"
        class="login-form"
        autocomplete="on"
        label-position="left"
      >
        <div class="title-container">
          <p class="tips">欢迎登录</p>
          <h3 class="title">{{ title }}</h3>
        </div>

        <a-form-model-item prop="userAccount" class="form-item">
          <a-input
            ref="userAccount"
            class="form-input"
            v-model="loginForm.userAccount"
            placeholder="请输入账号"
            @keydown.tab="handleTab"
          />
        </a-form-model-item>

        <a-form-model-item prop="userPassword" class="form-item">
          <a-input-password
            ref="userPassword"
            type="password"
            class="form-input-password"
            v-model="loginForm.userPassword"
            autocomplete="on"
            tabindex="2"
            @keyup.native="checkCapslock"
            @blur="capsTooltip = false"
            @keyup.enter.native="onSubmit"
            placeholder="请输入密码"
          />
        </a-form-model-item>
        <u-button :loading="loading" class="form-btn" block @click="onSubmit">
          登录
        </u-button>
      </a-form-model>
    </div>
  </div>
</template>
<script>
import { validatePassword, validateUsername } from "@/utils/validate";
import config from "@/config";

export default {
  data() {
    return {
      title: config.title,
      loginForm: {
        userAccount: "",
        userPassword: "",
      },
      rules: {
        userAccount: [
          { required: true, message: "请输入账号" },
          { trigger: "change", validator: validateUsername },
        ],
        userPassword: [
          { required: true, message: "请输入密码" },
          { trigger: "change", validator: validatePassword },
        ],
      },
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
    };
  },
  methods: {
    handleTab(e) {
      e.preventDefault();
      this.$refs.userPassword.focus();
    },
    onSubmit() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          this.loading = true;
          if (
            this.$store.state.users.find(
              (user) =>
                user.userAccount === this.loginForm.userAccount &&
                user.password === this.loginForm.userPassword
            )
          ) {
            localStorage.setItem("username", this.loginForm.userAccount);
            localStorage.setItem("password", this.loginForm.userPassword);
            this.$store.dispatch("login", {
              username: this.loginForm.userAccount,
              password: this.loginForm.userPassword,
            });
            this.$router.push({
              path: this.redirect || "/",
              query: this.otherQuery,
            });
          } else {
            this.$message.error("账号或者密码输入错误");
          }
          this.loading = false;
        }
      });
    },
    resetForm() {
      this.$refs.form.resetFields();
    },
    checkCapslock(e) {
      const { key } = e;
      this.capsTooltip = key && key.length === 1 && key >= "A" && key <= "Z";
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== "redirect") {
          acc[cur] = query[cur];
          // eslint-disable-next-line no-undef
        }
        return acc;
      }, {});
    },
  },
  mounted() {
    if (this.loginForm.username === "") {
      this.$refs.username.focus();
    } else if (this.loginForm.password === "") {
      this.$refs.password.focus();
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        const query = route.query;
        if (query) {
          this.redirect = query.redirect;
          this.otherQuery = this.getOtherQuery(query);
        }
      },
      immediate: true,
    },
  },
};
</script>

<style>
input:-internal-autofill-previewed,
input:-internal-autofill-selected {
  -webkit-text-fill-color: #212529 !important;
  transition: background-color 5000s ease-in-out 0s !important;
}
</style>
<style lang="scss" scoped>
$bg: #4f8eeb;
$bglogin: #fff;
$dark_gray: #889aa4;
$light_gray: #000;

.login-container {
  height: 100vh;
  background-image: url("../../assets/login-bg.jpg");
  overflow: hidden;
  background-repeat: no-repeat;
  background-size: 100% 100%;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding-right: 20vw;
  .login-detail {
    // width: 26%;
    // height: 56%;
    border-radius: 0.5rem;
    -webkit-box-shadow: 0 8px 16px rgba(29, 79, 166, 0.8);
    -moz-box-shadow: 0 8px 16px rgba(29, 79, 166, 0.8);
    -ms-box-shadow: 0 8px 16px rgba(29, 79, 166, 0.8);
    -o-box-shadow: 0 8px 16px rgba(29, 79, 166, 0.8);
    box-shadow: 0 8px 16px rgba(29, 79, 166, 0.8);
    background-color: #ffffff;
    display: flex;
  }

  .login-form {
    width: 360px;
    padding: 12% 14% 16%;

    .title-container {
      display: flex;
      flex-direction: column;
      margin-bottom: 10px;

      .title {
        font-size: 26px;
        color: #3f4346;
        margin: 0 0 8px 0;
        font-weight: bold;
        text-align: center;
      }
      .tips {
        font-size: 14px;
        margin-bottom: 10px;
        color: #67748e;
      }
    }
    .ant-form-item {
      margin-bottom: 22px;
      .ant-input {
        border-radius: 8px;
        height: 36px;
        background: #ffffff;
      }

      :deep .ant-form-item-label {
        padding-left: 11px;
      }
      :deep .ant-form-explain {
        position: absolute;
      }
    }

    .form-btn {
      width: 100%;
      height: 46px;
      line-height: 46px;
      border-radius: 0.6rem;
      margin-top: 10%;
      color: #fff;
    }
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 39px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }
}
</style>
